package com.android.aircondition.view;


import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.aircondition.R;
import com.android.aircondition.constant.AppConstant;
import com.android.aircondition.service.GreeService;
import com.android.aircondition.service.Mode;

public class SubTabModeView extends LinearLayout implements OnClickListener{
	
	private LinearLayout firstLinear;
	private LinearLayout secondLinear;
	private LinearLayout thirdLinear;
	private LinearLayout fourthLinear;
	private LinearLayout fifthLinear;
	
	private ImageView   firstImv;
	private ImageView   secondImv;
	private ImageView   thirdImv;
	private ImageView   fourthImv;
	private ImageView   fifthImv;
	
	private TextView    firstText;
	private TextView    secondText;
	private TextView    thirdText;
	private TextView    fourthText;
	private TextView    fifthText;	
	
	private View rootParent;
	
	private Boolean key;
	
	private GreeService service;
	
	public SubTabModeView(Context context) {
		super(context,null);
	}

	public SubTabModeView(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		LinearLayout linear = (LinearLayout)LayoutInflater.from(context).inflate(R.layout.sub_direction, this, true);
		  
		firstLinear  = (LinearLayout)linear.findViewById(R.id.Direction_subtab_first);
		secondLinear = (LinearLayout)linear.findViewById(R.id.Direction_subtab_second);
		thirdLinear  = (LinearLayout)linear.findViewById(R.id.Direction_subtab_third);
		fourthLinear = (LinearLayout)linear.findViewById(R.id.Direction_subtab_fourth);
		fifthLinear  = (LinearLayout)linear.findViewById(R.id.Direction_subtab_fifth);
		
		firstImv     = (ImageView   )linear.findViewById(R.id.sub_imv_first);
		secondImv    = (ImageView   )linear.findViewById(R.id.sub_imv_second);
		thirdImv     = (ImageView   )linear.findViewById(R.id.sub_imv_third);
		fourthImv    = (ImageView   )linear.findViewById(R.id.sub_imv_fourth);
		fifthImv     = (ImageView   )linear.findViewById(R.id.sub_imv_fifth);
		
		firstText    = (TextView    )linear.findViewById(R.id.sub_tv_first);
		secondText   = (TextView    )linear.findViewById(R.id.sub_tv_second);
		thirdText    = (TextView    )linear.findViewById(R.id.sub_tv_third);
		fourthText   = (TextView    )linear.findViewById(R.id.sub_tv_fourth);
		fifthText    = (TextView    )linear.findViewById(R.id.sub_tv_fifth);
		
		initData(context);
		
	}

	private void initData(Context context){
		
		firstImv .setImageResource(R.drawable.sub_tab_auto);
		secondImv.setImageResource(R.drawable.sub_tab_zhileng);
		thirdImv .setImageResource(R.drawable.sub_tab_songfeng);
		fourthImv.setImageResource(R.drawable.sub_tab_chushi);
		fifthImv .setImageResource(R.drawable.sub_tab_zhire);
		
		String[] array = context.getResources().getStringArray(R.array.sub_tab_mode);
		firstText .setText(array[AppConstant.NO_0]);
		secondText.setText(array[AppConstant.NO_1]);
		thirdText .setText(array[AppConstant.NO_2]);
		fourthText.setText(array[AppConstant.NO_3]);
		fifthText .setText(array[AppConstant.NO_4]);
		
		firstLinear .setOnClickListener(this);
		secondLinear.setOnClickListener(this);
		thirdLinear .setOnClickListener(this);
		fourthLinear.setOnClickListener(this);
		fifthLinear .setOnClickListener(this);
		
		service = new GreeService(context);
		Mode mode = service.getModePreference();
		
		int alphaAuto     = mode.getAuto()?255:150;
		int alphaZhileng  = mode.getZhileng()?255:150;
		int alphaSongfeng = mode.getSongfeng()?255:150;
		int alphaChushi   = mode.getChushi()?255:150;
		int alphaZhire    = mode.getZhire()?255:150;
		
		firstImv.setAlpha(alphaAuto);
		secondImv.setAlpha(alphaZhileng);
		thirdImv.setAlpha(alphaSongfeng);
		fourthImv.setAlpha(alphaChushi);
		fifthImv.setAlpha(alphaZhire);
		
		
	
		key = false;
	}
	
	public void onClick(View v) {
		
		if(key) return;
		
		key = true;
		
		Mode mode = service.getModePreference();
		
		switch (v.getId()) {
		
		case R.id.Direction_subtab_first:
			resetSubTabStatus();
			firstImv.setAlpha(255);
			mode.setAuto(true);
			break;
			
		case R.id.Direction_subtab_second:
			resetSubTabStatus();
			secondImv.setAlpha(255);
			mode.setZhileng(true);
			break;
			
		case R.id.Direction_subtab_third:
			resetSubTabStatus();
			thirdImv.setAlpha(255);
			mode.setSongfeng(true);
			break;
			
		case R.id.Direction_subtab_fourth:
			resetSubTabStatus();
			fourthImv.setAlpha(255);
			mode.setChushi(true);
			break;
			
		case R.id.Direction_subtab_fifth:
			resetSubTabStatus();
			fifthImv.setAlpha(255);
			mode.setZhire(true);
			break;

		}
		
		service.setModePreference(mode);
		
		key = false;
	}
	
	private void resetSubTabStatus(){
		firstImv .setAlpha(150);
		secondImv.setAlpha(150);
		thirdImv .setAlpha(150);
		fourthImv.setAlpha(150);
		fifthImv .setAlpha(150);
	}
	
	public void setFocusedMode(Mode mode){
		resetSubTabStatus();
		
		if(mode.getAuto())     firstImv .setAlpha(255);
		if(mode.getZhileng())  secondImv.setAlpha(255);
		if(mode.getSongfeng()) thirdImv .setAlpha(255);
		if(mode.getChushi())   fourthImv.setAlpha(255);
		if(mode.getZhire())    fifthImv .setAlpha(255);
	}

	public void setRootParent(View view){
		this.rootParent = view;
	}

	
}
